<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/7/3
 * Time: 上午11:16
 */

namespace app\src\mall\logic;


use app\src\base\logic\BaseLogic;
use app\src\mall\model\Mall;

class MallLogic extends BaseLogic
{

    protected function _init()
    {
        $this->setModel(new Mall());
    }


    /**
     * 添加商城商家
     * @param $entity
     * @return array
     */
    public function addMall($entity)
    {
        $data = [
            'name' => $entity['name'],
            'logo' => $entity['logo'],
            'synopsis' => $entity['synopsis'],
            'link' => isset($entity['link']) ? $entity['link'] : '',
            'other_info' => isset($entity['other']) ? json_encode($entity['other'], JSON_UNESCAPED_UNICODE) : '',
            'create_time' => NOW_TIME,
            'display_order' => isset($entity['display_order']) ? $entity['display_order'] : 0
        ];

        $result = $this->add($data);

        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 商城商家详情
     * @param $mall_id
     * @return array
     */
    public function mallDetail($mall_id)
    {
        $map = [
            'id' => $mall_id,
            'status' => 0
        ];
        $result = $this->getInfo($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(empty($result['info'])) return $this->apiReturnErr('error mall');

        $info = $result['info'];
        $info['other_info'] = empty($info['other_info']) ? [] : json_decode($info['other_info'], true);
        return $this->apiReturnSuc($info);
    }

    /**
     * 修改商城商家信息
     * @param $mall_id
     * @param $entity
     * @return array
     */
    public function editMall($mall_id, $entity)
    {
        $result = $this->mallDetail($mall_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);

        $this->saveByID($mall_id, $entity);

        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }

    }

    /**
     * 商城商家列表不分页
     * @return array
     */
    public function mallList()
    {

        $map = [
            'status' => 0
        ];

        $order = ['display_order' => 'desc', 'create_time' => 'asc'];
        $result = $this->queryNoPaging($map, $order);

        if($result['status']){
            foreach ($result['info'] as &$val){
                $val['other_info'] = empty($val['other_info']) ? [] : json_decode($val['other_info'], true);
            }
            return $this->apiReturnSuc($result['info']);
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }
}